package hl.action;

import java.io.IOException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import hl.pageModel.Card;
import hl.pageModel.DataGrid;
import hl.pageModel.Json;
import hl.service.CardService;


@Namespace("/")
@Action(value = "cardAction")
public class CardAction extends BaseAction implements ModelDriven<Card> {

	Card card = new Card();

	private CardService cardService;

	@Override
	public Card getModel() {
		return card;
	}

	public CardService getCardService() {
		return cardService;
	}

	@Autowired
	public void setCardService(CardService cardService) {
		this.cardService = cardService;
	}


	public void add() {
		Json j = new Json();
		try {
			cardService.save(card);
			j.setSuccess(true);
			j.setMsg("增加成功");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void getDatagrid() {
		DataGrid d = cardService.getDatagrid(card);
		super.writeJson(d);
	}

	public void edit() {
		Json j = new Json();
		try {
			cardService.edit(card);
			j.setSuccess(true);
			j.setMsg("修改成功！");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void remove() {
		Json j = new Json();
		try {
			cardService.remove(card);
			j.setSuccess(true);
			j.setMsg("删除成功！");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}
	
	public void getComboBox() {
		super.writeJson(cardService.getComboBox());
	}
	
	public void getCharts() throws IOException {
		super.writeJson(cardService.getCharts());
	}
}
